package com.guanyua.eims.service.impl;

import com.guanyua.eims.dao.PurchaseInfoMapper;
import com.guanyua.eims.pojo.salegoods.PurchaseGoods;
import com.guanyua.eims.dao.PurchaseGoodsMapper;
import com.guanyua.eims.pojo.salegoods.PurchaseGoodsAddForm;
import com.guanyua.eims.pojo.saleinfo.PurchaseInfo;
import com.guanyua.eims.service.PurchaseGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-10-18
 */
@Service
public class PurchaseGoodsServiceImpl extends ServiceImpl<PurchaseGoodsMapper, PurchaseGoods> implements PurchaseGoodsService {


    @Autowired
    PurchaseInfoMapper purchaseInfoMapper;
    @Autowired
    PurchaseGoodsService purchaseGoodsService;

    /**
     * 采购商品
     */
    @Override
    @Transactional
    public void purchaseGoods(PurchaseGoodsAddForm form) {
        //  添加采购信息
        PurchaseInfo purchaseInfo = new PurchaseInfo();
        purchaseInfoMapper.insert(purchaseInfo);

        //  添加采购商品
        List<PurchaseGoods> purchaseGoodsList = new ArrayList<>();
        for(PurchaseGoodsAddForm.Goods goods: form.getGoodsList()){
            PurchaseGoods purchaseGoods = new PurchaseGoods();
            BeanUtils.copyProperties(goods, purchaseGoods);
            purchaseGoods.setPurchaseId(purchaseInfo.getId());

            purchaseGoodsList.add(purchaseGoods);
        }

        purchaseGoodsService.saveBatch(purchaseGoodsList);
    }

}
